package com.dreamwhale.machinery.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.goods.entity.MachineBuy;
import com.dreamwhale.machinery.goods.entity.MachineSell;
import com.dreamwhale.machinery.goods.mapper.MachineSellMapper;
import com.dreamwhale.machinery.goods.service.MachineSellService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 二手机械出售 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@Service
public class MachineSellServiceImpl extends ServiceImpl<MachineSellMapper, MachineSell> implements MachineSellService {

    @Resource
    private MachineSellMapper machineSellMapper;

    @Override
    public int insertMachineSells(MachineSell machineSell) {
        machineSell.setCreateTime(LocalDateTime.now());
        return machineSellMapper.insert(machineSell);
    }

    @Override
    public List<MachineSell> selectMachineSellMap(Integer userId) {
        QueryWrapper<MachineSell> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("del_flag", 0);
        return machineSellMapper.selectList(queryWrapper);
    }

    @Override
    public List<MachineSell> selectMachineSells(Integer infoId) {
        QueryWrapper<MachineSell> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0).eq("id", infoId);
        return machineSellMapper.selectList(queryWrapper);
    }

    @Override
    public MachineSell selectMachineSellByTypeId(Integer infoId) {
        return machineSellMapper.selectMachineSellByTypeId(infoId);
    }

    @Override
    public List<MachineSell> selectMachineSellHome(Integer type, Integer types, Integer brandId, Integer typeId, String title) {
        QueryWrapper<MachineSell> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            queryWrapper.orderByDesc("count_number");
        }

        if (types != null) {
            queryWrapper.orderByDesc("create_time");
        }

        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }
        if (typeId != null) {
            queryWrapper.eq("type_id", typeId);
        }
        if (title != null) {
            queryWrapper.like("title", title);
        }

        queryWrapper.eq("del_flag", 0);
        return machineSellMapper.selectList(queryWrapper);
    }
}
